[COLLECTIONS-811] Integrate Guava Testlib tests for Apache Commons Collections#301
[COLLECTIONS-811] Integrate Guava Testlib tests for Apache Commons Collections#301garydgregory merged 2 commits intoapache:masterfrom
Conversation
|
/cc @kevinb9n 🙂 |
|
Rebased. Confirmed the license is compatible (AL as well). Waiting for CI, and will update |
|
And didn't notice a difference in build time. Executed a few times locally as well, and can confirm it doesn't bring any brittleness to our tests. Looks like a good addition to our test code. Thanks for showing us this @ben-manes ! |
| */ | ||
| public final class GuavaTestlibTest extends TestCase { | ||
|
|
||
| public static Test suite() { |
There was a problem hiding this comment.
Can this be done in the JUnit 4 or 5 style?
There was a problem hiding this comment.
I thought about that too, but I don't know how to translate the test suite created programmatically to JUnit 5 😥 ping @ben-manes
There was a problem hiding this comment.
The collection tests are JUnit3-based, and JUnit 4/5 have runners for supporting their older versions. Since those versions are not backwards compatible otherwise, it's a framework limitation. I think it is unlikely for Guava to revise the tests as it works well enough, even if old code.
FYI there are more test cases you could add (List, Set, etc) and I only did a quick check for Maps. (I also borrowed your tests for my implementation as another sanity check)
There was a problem hiding this comment.
Thanks @ben-manes !
I will leave the List, Set, etc., for follow-up issues. I had some 5 minutes to test it, and managed to write some tests for Lists, but these tests found issues in some of the Lists. I'd have to confirm if the features I selected are actually pertinent to the List implementations I used, or if we have other bugs 🙂
So it's definitely useful, but I prefer to get this merged first for Maps, and later add other Map, List, Set implementations too 👍
Here's the diff of what I had FWIW, thanks!!!
Bruno
diff --git a/src/test/java/org/apache/commons/collections4/GuavaTestlibTest.java b/src/test/java/org/apache/commons/collections4/GuavaTestlibTest.java
index 88108c6a..7d068e17 100644
--- a/src/test/java/org/apache/commons/collections4/GuavaTestlibTest.java
+++ b/src/test/java/org/apache/commons/collections4/GuavaTestlibTest.java
@@ -17,18 +17,25 @@
package org.apache.commons.collections4;
+import java.util.List;
import java.util.Map;
import java.util.function.Supplier;
+import org.apache.commons.collections4.list.CursorableLinkedList;
+import org.apache.commons.collections4.list.GrowthList;
+import org.apache.commons.collections4.list.TreeList;
import org.apache.commons.collections4.map.HashedMap;
import org.apache.commons.collections4.map.LRUMap;
import org.apache.commons.collections4.map.LinkedMap;
import org.apache.commons.collections4.map.ReferenceMap;
+import com.google.common.collect.testing.ListTestSuiteBuilder;
import com.google.common.collect.testing.MapTestSuiteBuilder;
+import com.google.common.collect.testing.TestStringListGenerator;
import com.google.common.collect.testing.TestStringMapGenerator;
import com.google.common.collect.testing.features.CollectionFeature;
import com.google.common.collect.testing.features.CollectionSize;
+import com.google.common.collect.testing.features.ListFeature;
import com.google.common.collect.testing.features.MapFeature;
import junit.framework.Test;
@@ -49,14 +56,17 @@ public final class GuavaTestlibTest extends TestCase {
public static Test suite() {
TestSuite test = new TestSuite();
- test.addTest(suite("HashedMap", HashedMap::new));
- test.addTest(suite("LinkedMap", LinkedMap::new));
- test.addTest(suite("LRUMap", LRUMap::new));
- test.addTest(suite("ReferenceMap", ReferenceMap::new));
+ test.addTest(suiteMap("HashedMap", HashedMap::new));
+ test.addTest(suiteMap("LinkedMap", LinkedMap::new));
+ test.addTest(suiteMap("LRUMap", LRUMap::new));
+ test.addTest(suiteMap("ReferenceMap", ReferenceMap::new));
+ test.addTest(suiteList("List", TreeList::new));
+ test.addTest(suiteList("GrowthList", GrowthList::new));
+ test.addTest(suiteList("CursorableLinkedList", CursorableLinkedList::new));
return test;
}
- public static Test suite(String name, Supplier<Map<String, String>> factory) {
+ public static Test suiteMap(String name, Supplier<Map<String, String>> factory) {
return MapTestSuiteBuilder.using(new TestStringMapGenerator() {
@Override
protected Map<String, String> create(Map.Entry<String, String>[] entries) {
@@ -73,4 +83,23 @@ public final class GuavaTestlibTest extends TestCase {
MapFeature.ALLOWS_ANY_NULL_QUERIES, CollectionFeature.SUPPORTS_ITERATOR_REMOVE)
.createTestSuite();
}
+
+ public static Test suiteList(String name, Supplier<List<String>> factory) {
+ return ListTestSuiteBuilder.using(new TestStringListGenerator() {
+ @Override
+ protected List<String> create(String[] elements) {
+ List<String> list = factory.get();
+ for (String element : elements) {
+ list.add(element);
+ }
+ return list;
+ }
+ })
+ .named(name)
+ .withFeatures(
+ CollectionSize.ANY, ListFeature.GENERAL_PURPOSE,
+ ListFeature.REMOVE_OPERATIONS, ListFeature.SUPPORTS_REMOVE_WITH_INDEX,
+ CollectionFeature.SUPPORTS_ITERATOR_REMOVE)
+ .createTestSuite();
+ }
}There was a problem hiding this comment.
I can get a little further using the following, where TreeList passes but the others don't. The remaining failures look like actual mistakes.
public static Test suite() {
TestSuite test = new TestSuite();
test.addTest(suiteList("TreeList", TreeList::new));
test.addTest(suiteList("GrowthList", GrowthList::new, CollectionFeature.SERIALIZABLE));
test.addTest(suiteList("CursorableLinkedList", CursorableLinkedList::new, CollectionFeature.SERIALIZABLE));
return test;
}
public static Test suiteList(String name, Supplier<List<String>> factory, Feature<?>... features) {
var suite = ListTestSuiteBuilder.using(new TestStringListGenerator() {
@Override
protected List<String> create(String[] elements) {
List<String> list = factory.get();
for (String element : elements) {
list.add(element);
}
return list;
}
}).named(name)
.withFeatures(CollectionSize.ANY,
ListFeature.GENERAL_PURPOSE,
ListFeature.REMOVE_OPERATIONS,
CollectionFeature.ALLOWS_NULL_VALUES,
CollectionFeature.DESCENDING_VIEW,
CollectionFeature.SUBSET_VIEW);
suite.withFeatures(features);
return suite.createTestSuite();
}There was a problem hiding this comment.
Yeah, same for me with your code. I'm updating the PR now with the suiteMap and suiteTest methods, but leaving only TreeList tests enabled for now. I left a TODO marker so we can revisit it later.
Thanks!
There was a problem hiding this comment.
At least in the case of GrowthList some of the failures are expected since it modified the List contract,
* Decorates another <code>List</code> to make it seamlessly grow when
* indices larger than the list size are used on add and set,
* avoiding most IndexOutOfBoundsExceptions.Technically a violation of the contract, but expected.
* @throws IndexOutOfBoundsException if the index is out of range
* ({@code index < 0 || index > size()})
*/
void add(int index, E element);You can use .suppressing(Method...) to disable those cases.
|
Rebased. |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #301 +/- ##
============================================
+ Coverage 85.95% 86.06% +0.10%
- Complexity 4670 4672 +2
============================================
Files 292 292
Lines 13467 13467
Branches 1954 1954
============================================
+ Hits 11576 11590 +14
+ Misses 1325 1320 -5
+ Partials 566 557 -9 ☔ View full report in Codecov by Sentry. |
| * This test uses Google's Guava Testlib testing libraries to validate the | ||
| * contract of collection classes in Commons Collections. This was introduced | ||
| * after COLLECTIONS-802, where the issue reported was found with Testlib, | ||
| * with thanks to Ben Manes. |
There was a problem hiding this comment.
We don't use @author tags, so would you please move attribution to changes.xml and/or pom.xml?
There was a problem hiding this comment.
I am fine having it removed and appreciate the thanks.
…llections (apache#301) * [COLLECTIONS-811] Integrate Guava Testlib tests for Apache Commons Collections * [COLLECTIONS-811] Add tests for Lists too, thanks to @ben-manes
Adding the test class from @ben-manes, pending merge of #300